package com.windward.vii.web.controller;

import com.windward.vii.common.JsonResult;
import com.windward.vii.common.ResultCode;
import com.windward.vii.service.ChatService;
import com.windward.vii.service.UserTokenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by tiansj on 2017/8/26.
 */
@Controller
public class ChatController {

    private static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Resource
    UserTokenService userTokenService;
    @Resource
    ChatService chatService;

    /**
     * 随机聊天
     * @param token
     * @return 返回主播UID
     */
    @RequestMapping(value = "v1/api1/user/chat/random", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult randomChat(Integer sex, @RequestHeader(value="token") String token) {
        Long uid = userTokenService.getUidByToken(token);
        return chatService.randomChatV2(uid, sex);
    }

    /**
     * 终止随机聊天
     * @param token
     * @return 返回主播UID
     */
    @RequestMapping(value = "v1/api1/user/chat/random/abort", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult randomAbort(@RequestHeader(value="token") String token) {
        Long uid = userTokenService.getUidByToken(token);
        return chatService.randomAbortV2(uid);
    }

    /**
     * 用户聊天中消费
     * @param anchorUid
     * @param token
     * @return
     */
    @RequestMapping(value = "v1/api1/user/chat/consume", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult consume(@RequestParam Long chatId,
                              @RequestParam Long anchorUid,
                              @RequestHeader(value="token") String token,
                              @RequestHeader(value="User-Agent") String userAgent) {
        if(anchorUid <= 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "聊天用户不存在，targetUid错误");
        }
        Long uid = userTokenService.getUidByToken(token);
        logger.info("User-Agent:{}", userAgent);
        return chatService.consume(chatId, uid, anchorUid, userAgent);
    }

    /**
     * 用户连接主播
     * @param anchorUid
     * @param token
     * @return
     */
    @RequestMapping(value = "v1/api1/user/chat/connect", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult connect(@RequestParam Long anchorUid,
                              @RequestHeader(value="token") String token,
                              @RequestHeader(value="User-Agent") String userAgent) {
        if(anchorUid <= 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "聊天用户不存在，targetUid错误");
        }
        Long uid = userTokenService.getUidByToken(token);
        return chatService.connect(uid, anchorUid, false, 0, userAgent);
    }

    /**
     * 用户连接主播，连接成功后，发送push
     * @param anchorUid
     * @param token
     * @return
     */
    @RequestMapping(value = "v2/api1/user/chat/connect", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult connectV2(@RequestParam Long anchorUid,
                                @RequestHeader(value="token") String token,
                                Integer liveMode,
                                HttpServletRequest request,
                                @RequestHeader(value="User-Agent") String userAgent) {
        String appInfo = request.getHeader("App-Info");
        logger.info("App-Info:{}", appInfo);
        if(anchorUid <= 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "聊天用户不存在，targetUid错误");
        }
        Long uid = userTokenService.getUidByToken(token);
        if(liveMode == null) {
            liveMode = 0;
        }
        return chatService.connect(uid, anchorUid, true, liveMode, userAgent);
    }

//    /**
//     * 用户取消建立连接
//     * @param chatId
//     * @param token
//     * @return
//     */
//    @RequestMapping(value = "v1/api1/user/chat/cancel", method = RequestMethod.POST)
//    @ResponseBody
//    public JsonResult cancel(@RequestParam Long chatId,
//                             @RequestParam Long anchorUid,
//                             @RequestHeader(value="token") String token) {
//        if(anchorUid <= 0) {
//            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "聊天用户不存在，targetUid错误");
//        }
//        Long uid = userTokenService.getUidByToken(token);
//        return chatService.cancel(chatId, uid, anchorUid);
//    }
//
//    /**
//     * 用户终止聊天
//     * @param anchorUid
//     * @param token
//     * @return
//     */
//    @RequestMapping(value = "v1/api1/user/chat/abort", method = RequestMethod.POST)
//    @ResponseBody
//    public JsonResult abort(@RequestParam Long chatId,
//                            @RequestParam Long anchorUid,
//                            @RequestHeader(value="token") String token) {
//        if(anchorUid <= 0) {
//            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "聊天用户不存在，targetUid错误");
//        }
//        Long uid = userTokenService.getUidByToken(token);
//        return chatService.abortByUser(chatId, uid, anchorUid);
//    }

    /**
     * 主播接收聊天
     * @param uid
     * @param token
     * @return
     */
    @RequestMapping(value = "v1/api1/anchor/chat/accept", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult accept(@RequestParam Long chatId,
                             @RequestParam Long uid,
                             @RequestHeader(value="token") String token) {
        if(chatId <= 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "聊天关系不存在，chatId错误");
        }
        if(uid <= 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "聊天用户不存在，uid错误");
        }
        Long anchorUid = userTokenService.getUidByToken(token);
        return chatService.accept(chatId, anchorUid, uid);
    }

    @RequestMapping(value = "v1/api1/anchor/chat/accept/check", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult acceptCheck(@RequestParam Long chatId,
                             @RequestParam Long uid,
                             @RequestHeader(value="token") String token) {
        if(chatId <= 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "聊天关系不存在，chatId错误");
        }
        if(uid <= 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "聊天用户不存在，uid错误");
        }
        Long anchorUid = userTokenService.getUidByToken(token);
        return chatService.acceptCheck(chatId, anchorUid, uid);
    }

//    /**
//     * 主播拒绝聊天
//     * @param uid
//     * @param token
//     * @return
//     */
//    @RequestMapping(value = "v1/api1/anchor/chat/refuse", method = RequestMethod.POST)
//    @ResponseBody
//    public JsonResult refuse(@RequestParam Long uid,
//                             @RequestHeader(value="token") String token) {
//        if(uid <= 0) {
//            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "聊天用户不存在，targetUid错误");
//        }
//        Long anchorUid = userTokenService.getUidByToken(token);
//        return chatService.refuse(anchorUid, uid);
//    }

//    /**
//     * 主播终止聊天
//     * @param uid
//     * @param token
//     * @return
//     */
//    @RequestMapping(value = "v1/api1/anchor/chat/abort", method = RequestMethod.POST)
//    @ResponseBody
//    public JsonResult actorAbort(@RequestParam Long uid,
//                             @RequestHeader(value="token") String token) {
//        Long anchorUid = userTokenService.getUidByToken(token);
//        return chatService.abortByAnchor(anchorUid, uid);
//    }


    @RequestMapping(value = "v1/api1/chat/abort", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult abort(@RequestParam Long chatId,
                            @RequestHeader(value="token") String token,
                            @RequestHeader(value="User-Agent") String userAgent) {
        Long uid = userTokenService.getUidByToken(token);
        return chatService.abort(chatId, uid, userAgent);
    }

    @RequestMapping(value = "v1/api1/chat/keepalive", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult keepalive(@RequestParam Long chatId,
                            @RequestHeader(value="token") String token) {
        return JsonResult.success();
    }

}
